#include <buola/scene/cgeode.h>
#include <buola/scene/cmesh.h>
#include <buola/io/curi.h>
#include <buola/scene/crendercontext.h>
#include <buola/scene/ctransform.h>
#include <buola/scene/techniques/cmeshtechnique.h>
#include <buola/scene/techniques/cmeshtitechnique.h>
#include <buola/scene/cmaterial.h>

#include "import/civparser.h"

namespace buola { namespace scene {

CGeode::CGeode()
    :   mTechnique(CMeshTechnique::Get())
    ,   mTITechnique(CMeshTITechnique::Get())
{
}

CGeode::CGeode(PMesh pMesh)
    :   mMesh(pMesh)
    ,   mTechnique(CMeshTechnique::Get())
    ,   mColor(0.7,0.7,0.7)
{
}

CGeode::~CGeode()
{
}

void CGeode::Render(CRenderContext &pContext)
{
    ///\todo set model matrix if we have the transform stored in the geode instead of some other object
    if(pContext.mTransformIndex)
        mTITechnique->Prepare(pContext);
    else
    {
        if(mMaterial)
            mMaterial->Prepare(pContext);
        else
            mTechnique->Prepare(pContext);
    }

    gl::vertex_attrib(1,mColor);
    mMesh->Render();
}

void CGeode::CollectPolygons(std::vector<mat::CVec3d> &pPoints,std::vector<mat::CVec3d> &pNormals,std::vector<int> &pFaces)
{
    mMesh->CollectPolygons(pPoints,pNormals,pFaces,mTransform->GetWorldTransform());
}

PGeode CGeode::Import(const io::CURI &pURI,double pScale)
{
    PMesh lMesh=CMesh::Import(pURI,pScale);
    CGeode *lGeode=new CGeode;
    lGeode->mMesh=lMesh;
    return lGeode;
}

void CGeode::SetMaterial(const PMaterial &pMaterial)
{
    mMaterial=pMaterial;
}

/*namespace gl*/ } /*namespace buola*/ }
